package com.asj.vivo.lock.utils;


import static com.asj.enums.NotificationSwitchEnums.LOCK_NOTIFICATION;
import static com.asj.enums.NotificationSwitchEnums.PUBLIC_NOTIFICATION;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.asj.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具类
 * */
public class CommonsUtils {
    //筛选IMEI号
    public static String screenIMEI(String content){
        //正则匹配，筛选一个连续或单个的数字
        Matcher matcher = Pattern.compile("\\d+").matcher(content);

        // 用于存储所有匹配到的数字
        StringBuilder imei = new StringBuilder();

        // 查找并追加所有匹配的数字
        while (matcher.find()) {
            imei.append(matcher.group());
        }
        return imei.toString();
    }

    //解析获取DeviceCode
    public static String getDeviceCode(String imei) {
        return screenIMEI(imei.split(",")[0]);
    }

    /**
     * 发布通知的方法
     * */
    public static int showNotification(@NonNull Context context, @NonNull String title, @NonNull String content) {
        // 获取系统服务来发布通知
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // 创建通知渠道（Android 8.0及以上）
        CharSequence channelName = PUBLIC_NOTIFICATION.getRemark();
        String channelId = PUBLIC_NOTIFICATION.getValue();
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setDescription("监管通知");
        notificationManager.createNotificationChannel(channel);

        // 构建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_launcher) // 设置通知图标
                .setContentTitle(title) // 设置通知标题
                .setContentText(content) // 设置通知内容
                .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 设置通知优先级
                .setAutoCancel(true); // 点击通知后自动取消

        // 如果需要，可以设置点击通知后打开的Intent
        /*Intent intent = new Intent(context, MainActivity.class); // 根据需要修改目标Activity
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 132, intent, PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);*/

        // 发布通知
        int notificationId = (int) System.currentTimeMillis();
        notificationManager.notify(notificationId, builder.build());
        return notificationId;
    }

    /**
     * 关闭指定通知的方法
     * */
    public static void cancelNotificationById(@NonNull Context context, int notificationId) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(notificationId); // 取消指定 ID 的通知
    }

    /**
     * 构建打电话专属通知
     * */
    public static void showPersistentCallNotification(Context context, String title, String content, String phoneNumber) {
        String CHANNEL_ID = LOCK_NOTIFICATION.getValue();
        // 创建通知管理器
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // 创建通知渠道（适用于 Android 8.0 及以上）
        CharSequence channelName = "锁机通知";
        String channelDescription = "通过该通知提醒用户锁机，同时点击即可拨打号码";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
        channel.setDescription(channelDescription);
        notificationManager.createNotificationChannel(channel);

        // 创建拨打电话的意图
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + phoneNumber));

        // 创建 PendingIntent
        PendingIntent pendingIntent;
        pendingIntent = PendingIntent.getActivity(context, 0, callIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        // 构建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setOngoing(true); // 设置为固有化不可删除

        // 显示通知
        Notification notification = builder.build();
        notificationManager.notify(LOCK_NOTIFICATION.getCode(), notification);
    }
}
